<?php

use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;
use kartik\select2\Select2;
use backend\models\Contract;
use backend\models\Contractor;
use backend\models\FinanceSource;
use backend\models\ContractStatus;
use backend\models\ContractPurpose; 
use backend\models\Donor;
use backend\models\Project;
use backend\models\ContractPurchaseMethod;
use backend\models\OrganizationBranch;
use Da\User\Model\User;

/* @var $this yii\web\View */
/* @var $model backend\models\ContractSearch */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="search-form contract-search collapse" id="js-collapse-search">

    <?php $form = ActiveForm::begin([
        'action' => ['index'],
        'method' => 'get',
    ]); ?>

    <div class="row">
        <?= $form->field($model, 'id', ['options' => ['class' => 'col-sm-3']]) ?>
        <?= $form->field($model, 'registry_number',  ['options' => ['class' => 'col-sm-3']]) ?>
    </div>

    <div class="row">
        <?= $form->field($model, 'own_number',  ['options' => ['class' => 'col-sm-3']]) ?>
        <?= $form->field($model, 'contractor_number',  ['options' => ['class' => 'col-sm-3']]) ?>
        <?= $form->field($model, 'id_status', ['options' => ['class' => 'col-sm-3']])->dropdownList(
            ContractStatus::find()->select(['title', 'id'])->indexBy('id')->orderBy('sort')->column(),
            [
                'prompt' => 'Выберете стутус договора...'
            ])
        ?>
    </div>

    <br>

    <?= $form->field($model, 'id_contractor')->widget(Select2::classname(), [
            'name' => 'contractor_in_serch',
            'data' => Contractor::contractorList(),
            'options' => ['placeholder' => 'Выберете контрагента...', 'id' => 'select2-contractor__search'],
            
        ]); 
    ?>

    <div class="row">
        <?= $form->field($model, 'contractorInn',  ['options' => ['class' => 'col-sm-3']]) ?>
        <?= $form->field($model, 'contractorKpp',  ['options' => ['class' => 'col-sm-3']]) ?>
    </div>

    <br>

    <?= $form->field($model, 'subject') ?>

    <div class="row">
        <?= $form->field($model, 'id_finance_source', ['options' => ['class' => 'col-sm-4']])->dropdownList(
            FinanceSource::financeSourceList(),
            [
                'prompt' => 'Выберете источник финансирования...'
            ])
        ?>
        <?= $form->field($model, 'id_purchase_method', ['options' => ['class' => 'col-sm-4']])->dropdownList(
            ContractPurchaseMethod::find()->select(['title', 'id'])->indexBy('id')->column(),
            [
                'prompt' => 'Выберете способ закупки...'
            ])
        ?> 
        <?= $form->field($model, 'id_purpose', ['options' => ['class' => 'col-sm-4']])->dropdownList(
            ContractPurpose::find()->select(['title', 'id'])->indexBy('id')->column(),
            [
                'prompt' => 'Выберете цель закупки...'
            ])
        ?> 
    </div>
    
    <br>

    <?php echo $form->field($model, 'total_price') ?>
    
    <div class="row">
        <?= $form->field($model, 'totalPriceMore',  ['options' => ['class' => 'col-sm-6']]) ?>
        <?= $form->field($model, 'totalPriceLess', ['options' => ['class' => 'col-sm-6']]) ?>
    </div>

    <br>

    <div class="row">
        <?= $form->field($model, 'id_donor', ['options' => ['class' => 'col-sm-6']])->widget(Select2::classname(), [
                'data' => Donor::find()->select(['name', 'id'])->indexBy('id')->column(),
                'options' => ['placeholder' => 'Выберете донора...'],
            ]); 
        ?>
        <?= $form->field($model, 'id_project', ['options' => ['class' => 'col-sm-6']])->widget(Select2::classname(), [
                'data' => Project::find()->select(['title', 'id'])->where(['!=', 'status', Project::STATUS_PLANNED])->indexBy('id')->column(),
                'options' => ['placeholder' => 'Выберете проект...'],
            ]); 
        ?>
    </div>

    <div class="row">
        <?php echo $form->field($model, 'dateSignStart', ['options' => ['class' => 'col-sm-3']])->input('date') ?>
        <?php echo $form->field($model, 'dateSignEnd', ['options' => ['class' => 'col-sm-3']])->input('date') ?>
        
        <?php echo $form->field($model, 'dateRealizeStart', ['options' => ['class' => 'col-sm-3']])->input('date') ?>
        <?php echo $form->field($model, 'dateRealizeEnd', ['options' => ['class' => 'col-sm-3']])->input('date') ?>
    </div>

    <?= $form->field($model, 'id_liable_user')->widget(Select2::classname(), [
            'data' => ArrayHelper::map(User::find()->with('profile')->all(), 'id', 'profile.name'),
            'options' => ['placeholder' => 'Выберите сотрудника...'],
        ]); 
    ?>

    <?= $form->field($model, 'id_organization_branch')->dropdownList(
            OrganizationBranch::find()->select(['title', 'id'])->indexBy('id')->column(),
            [
                'prompt' => 'Выберете филиал...'
            ]
        );
    ?>

    <?= $form->field($model, 'is_yearly')->radioList(
        [
            Contract::ONE_TIME => 'Нет',
            Contract::YEAR_TIME => 'Да',
        ])
    ?>

    <div class="form-group">
        <?= Html::submitButton('Поиск', ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Сбросить', ['index'], ['class' => 'btn btn-secondary']) ?>
    </div>

    <?php ActiveForm::end(); ?>

</div>
